from rest_framework import status,viewsets
from rest_framework.decorators import action
from rest_framework.response import Response


from ERP_system.utils.multiple_destory import MultipleDestoryMixin
from ERP_system.utils.pagination import GlobalPagination
from  basic_info .models import  Nation,Province,City
from basic_info.serializer.area_serializer import NationSerializer,ProvinceSerializer,CitySerializer


class NationView(viewsets.ModelViewSet,MultipleDestoryMixin):


    queryset = Nation.objects.all()
    serializer_class = NationSerializer


class ProvinceView(viewsets.ModelViewSet,MultipleDestoryMixin):

    queryset = Province.objects.all()
    serializer_class = ProvinceSerializer


    def get_queryset(self):
        #获取请求参数nid，表示依据哪个国家查询该国家的省份

       nid=self.request.query_params.get('nid',None)

       if nid:
           nid=int(nid)

           return Province.objects.filter(nation__id=nid).all()

       else:
           return Province.objects.all()

   
class CityView(viewsets.ModelViewSet,MultipleDestoryMixin):

    queryset = City.objects.all()
    serializer_class = CitySerializer


    def get_queryset(self):
        #获取请求参数pid，表示依据哪个省份查询该省份的国家

       pid=self.request.query_params.get('pid',None)

       if pid:
           nid=int(pid)

           return City.objects.filter(province__id=pid).all()

       else:
           return City.objects.all()

